import 'dart:core';

import 'package:ball_lion/utils/utils.dart';

import 'football/zq_schedule_entity.dart';

class PointsEntity {
  String? introduce;
  List<MatchStage>? matchStages;

  PointsEntity({
    this.introduce,
    this.matchStages,
  });

  PointsEntity.fromSoccer(Map<String, dynamic> json) {
    introduce = json['introduce'];
    if (json['list'] != null) {
      matchStages = <MatchStage>[];
      json['list'].forEach((v) {
        matchStages!.add(new MatchStage.fromSoccer(v));
      });
    }
  }

  PointsEntity.fromBasketball(Map<String, dynamic> json) {
    introduce = json['introduce'];
    if (json['list'] != null) {
      matchStages = <MatchStage>[];
      json['list'].forEach((v) {
        matchStages!.add(new MatchStage.fromBasketball(v));
      });
    }
  }
}

class MatchStage {
  String? name;
  Road? road;
  List<Schedules>? schedules;
  List<MatchGroup>? matchGroups;
  List<MatchGroup>? areaGroups;

  MatchStage(
      {this.name,
      this.road,
      this.schedules,
      this.matchGroups,
      this.areaGroups});

  MatchStage.fromSoccer(Map<String, dynamic> json) {
    name = json['name'];
    road = json['road'] != null ? new Road.fromSoccer(json['road']) : null;
    if (json['list'] != null) {
      matchGroups = <MatchGroup>[];
      json['list'].forEach((v) {
        matchGroups!.add(new MatchGroup.fromSoccer(v));
      });
    }
  }

  MatchStage.fromBasketball(Map<String, dynamic> json) {
    name = json['name'];
    road = json['road'] != null ? new Road.fromBasketball(json['road']) : null;
    if (json['rankGroupList'] != null) {
      matchGroups = <MatchGroup>[];
      json['rankGroupList'].forEach((v) {
        matchGroups!.add(new MatchGroup.fromBasketball(v));
      });
    }
    if (json['rankGroupList2'] != null) {
      areaGroups = <MatchGroup>[];
      json['rankGroupList2'].forEach((v) {
        areaGroups!.add(new MatchGroup.fromBasketball(v));
      });
    }
    if (json['scheduleList'] != null) {
      schedules = <Schedules>[];
      json['scheduleList'].forEach((v) {
        schedules!.add(new Schedules.fromBasketball(v));
      });
    }
  }
}

class Road {
  List<ScheduleGroup>? roadVs;
  List<ScheduleGroup>? roadVs16;
  List<ScheduleGroup>? roadVs4;
  List<ScheduleGroup>? roadVs8;

  Road({this.roadVs, this.roadVs16, this.roadVs4, this.roadVs8});

  Road.fromSoccer(Map<String, dynamic> json) {
    if (json['roadVs'] != null) {
      roadVs = <ScheduleGroup>[];
      json['roadVs'].forEach((v) {
        roadVs!.add(new ScheduleGroup.fromSoccer(v));
      });
    }
    if (json['roadVs16'] != null) {
      roadVs16 = <ScheduleGroup>[];
      json['roadVs16'].forEach((v) {
        roadVs16!.add(new ScheduleGroup.fromSoccer(v));
      });
    }
    if (json['roadVs4'] != null) {
      roadVs4 = <ScheduleGroup>[];
      json['roadVs4'].forEach((v) {
        roadVs4!.add(new ScheduleGroup.fromSoccer(v));
      });
    }
    if (json['roadVs8'] != null) {
      roadVs8 = <ScheduleGroup>[];
      json['roadVs8'].forEach((v) {
        roadVs8!.add(new ScheduleGroup.fromSoccer(v));
      });
    }
  }

  Road.fromBasketball(Map<String, dynamic> json) {
    if (json['roadVs'] != null) {
      roadVs = <ScheduleGroup>[];
      json['roadVs'].forEach((v) {
        roadVs!.add(new ScheduleGroup.fromBasketball(v));
      });
    }
    if (json['roadVs16'] != null) {
      roadVs16 = <ScheduleGroup>[];
      json['roadVs16'].forEach((v) {
        roadVs16!.add(new ScheduleGroup.fromBasketball(v));
      });
    }
    if (json['roadVs4'] != null) {
      roadVs4 = <ScheduleGroup>[];
      json['roadVs4'].forEach((v) {
        roadVs4!.add(new ScheduleGroup.fromBasketball(v));
      });
    }
    if (json['roadVs8'] != null) {
      roadVs8 = <ScheduleGroup>[];
      json['roadVs8'].forEach((v) {
        roadVs8!.add(new ScheduleGroup.fromBasketball(v));
      });
    }
  }

  Map<String, dynamic> toJsonSoccer() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.roadVs != null) {
      data['roadVs'] = this.roadVs!.map((v) => v.toJsonSoccer()).toList();
    }
    if (this.roadVs16 != null) {
      data['roadVs16'] = this.roadVs16!.map((v) => v.toJsonSoccer()).toList();
    }
    if (this.roadVs4 != null) {
      data['roadVs4'] = this.roadVs4!.map((v) => v.toJsonSoccer()).toList();
    }
    if (this.roadVs8 != null) {
      data['roadVs8'] = this.roadVs8!.map((v) => v.toJsonSoccer()).toList();
    }
    return data;
  }

  Map<String, dynamic> toJsonBasketball() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.roadVs != null) {
      data['roadVs'] = this.roadVs!.map((v) => v.toJsonBasketball()).toList();
    }
    if (this.roadVs16 != null) {
      data['roadVs16'] =
          this.roadVs16!.map((v) => v.toJsonBasketball()).toList();
    }
    if (this.roadVs4 != null) {
      data['roadVs4'] = this.roadVs4!.map((v) => v.toJsonBasketball()).toList();
    }
    if (this.roadVs8 != null) {
      data['roadVs8'] = this.roadVs8!.map((v) => v.toJsonBasketball()).toList();
    }
    return data;
  }
}

class MatchGroup {
  String? name;
  List<ScheduleGroup>? scheduleGroups;
  List<RankingTeam>? teams;
  List<RankingQualify>? qualify;

  MatchGroup.fromSoccer(Map<String, dynamic> json) {
    name = json['name'];
    if (json['scheduleList'] != null) {
      scheduleGroups = <ScheduleGroup>[];
      json['scheduleList'].forEach((v) {
        scheduleGroups!.add(new ScheduleGroup.fromSoccer(v));
      });
    }
    if (json['standingList'] != null) {
      qualify = <RankingQualify>[];
      json['standingList']?['qualifying'].forEach((v) {
        qualify!.add(new RankingQualify.fromSoccer(v));
      });
      teams = <RankingTeam>[];
      json['standingList']?['teamStanding'].forEach((v) {
        teams!.add(new RankingTeam.fromSoccer(v));
      });
    }
  }

  MatchGroup.fromBasketball(Map<String, dynamic> json) {
    name = json['name'];
    if (json['lineList'] != null) {
      qualify = <RankingQualify>[];
      json['lineList'].forEach((v) {
        qualify!.add(new RankingQualify.fromBasketball(v));
      });
    }
    if (json['rankList'] != null) {
      teams = <RankingTeam>[];
      json['rankList'].forEach((v) {
        teams!.add(new RankingTeam.fromBasketball(v));
      });
    }
  }
}

class ScheduleGroup {
  int? sort;
  String? name;
  int? winTeamId;
  String? roundName;
  List<Schedules>? schedules;
  int? rightId;
  String? rightName;
  String? rightLogo;
  int? rightScore;
  int? rightScorePK;
  int? rightScoreOT;
  int? leftId;
  String? leftName;
  String? leftLogo;
  int? leftScore;
  int? leftScorePK;
  int? leftScoreOT;

  ScheduleGroup({
    this.sort,
    this.name,
    this.winTeamId,
    this.schedules,
    this.rightId,
    this.rightName,
    this.rightLogo,
    this.rightScore,
    this.leftId,
    this.leftName,
    this.leftLogo,
    this.leftScore,
  });

  ScheduleGroup.fromSoccer(Map<String, dynamic> json) {
    sort = json['sort'];
    name = json['name'];
    winTeamId = json['winTeamId'];
    if (json['scheduleList'] != null) {
      schedules = <Schedules>[];
      json['scheduleList'].forEach((v) {
        schedules!.add(new Schedules.fromSoccer(v));
      });
    }
    if (schedules.hasValue) {
      rightId = schedules![0].rightId;
      rightName = schedules![0].rightName;
      rightLogo = schedules![0].rightLogo;
      leftId = schedules![0].leftId;
      leftName = schedules![0].leftName;
      leftLogo = schedules![0].leftLogo;
      roundName = schedules![0].leagueName ?? "";
      if (schedules?.every((element) => element.status == 7) == true) {
        leftScore = rightScore = 0;
        if (schedules?.any((element) =>
                element.leftScoreOT != null || element.rightScoreOT != null) ==
            true) {
          leftScoreOT = rightScoreOT = 0;
        }
        if (schedules?.any((element) =>
                element.leftScorePK != null || element.rightScorePK != null) ==
            true) {
          leftScorePK = rightScorePK = 0;
        }
        for (int i = 0; i < schedules!.length; i++) {
          if (i % 2 == 0) {
            leftScore = leftScore! + (schedules![i].leftScoreEnd ?? 0);
            rightScore = rightScore! + (schedules![i].rightScoreEnd ?? 0);
            if (leftScoreOT != null) {
              leftScoreOT = leftScoreOT! + (schedules![i].leftScoreOT ?? 0);
              rightScoreOT = rightScoreOT! + (schedules![i].rightScoreOT ?? 0);
            }
            if (leftScorePK != null) {
              leftScorePK = leftScorePK! + (schedules![i].leftScorePK ?? 0);
              rightScorePK = rightScorePK! + (schedules![i].rightScorePK ?? 0);
            }
          } else {
            leftScore = leftScore! + (schedules![i].rightScoreEnd ?? 0);
            rightScore = rightScore! + (schedules![i].leftScoreEnd ?? 0);
            if (leftScoreOT != null) {
              leftScoreOT = leftScoreOT! + (schedules![i].rightScoreOT ?? 0);
              rightScoreOT = rightScoreOT! + (schedules![i].leftScoreOT ?? 0);
            }
            if (leftScorePK != null) {
              leftScorePK = leftScorePK! + (schedules![i].rightScorePK ?? 0);
              rightScorePK = rightScorePK! + (schedules![i].leftScorePK ?? 0);
            }
          }
        }
      }
    }
  }

  ScheduleGroup.fromBasketball(Map<String, dynamic> json) {
    sort = json['sort'];
    name = json['name'];
    winTeamId = json['winTeamId'];
    roundName = json['name'];
    rightId = json['awayTeamId'];
    rightName = json['awayName'];
    rightLogo = json['awayLogo'];
    rightScore = json['awayScore'];
    leftId = json['homeTeamId'];
    leftName = json['homeName'];
    leftLogo = json['homeLogo'];
    leftScore = json['homeScore'];
    // rightId = json['homeTeamId'];
    // rightName = json['homeName'];
    // rightLogo = json['homeLogo'];
    // rightScore = json['homeScore'];
    // leftId = json['awayTeamId'];
    // leftName = json['awayName'];
    // leftLogo = json['awayLogo'];
    // leftScore = json['awayScore'];
    if (json['list'] != null) {
      schedules = <Schedules>[];
      json['list'].forEach((v) {
        schedules!.add(new Schedules.fromBasketball(v));
      });
    }
  }

  Map<String, dynamic> toJsonSoccer() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['name'] = this.name;
    data['winTeamId'] = this.winTeamId;
    if (this.schedules != null) {
      data['scheduleList'] =
          this.schedules!.map((v) => v.toJsonSoccer()).toList();
    }
    return data;
  }

  Map<String, dynamic> toJsonBasketball() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['name'] = this.name;
    data['winTeamId'] = this.winTeamId;
    data['rightId'] = this.rightId;
    data['rightName'] = this.rightName;
    data['rightLogo'] = this.rightLogo;
    data['rightScore'] = this.rightScore;
    data['leftId'] = this.leftId;
    data['leftName'] = this.leftName;
    data['leftLogo'] = this.leftLogo;
    data['leftScore'] = this.leftScore;
    if (this.schedules != null) {
      data['list'] = this.schedules!.map((v) => v.toJsonBasketball()).toList();
    }
    return data;
  }
}

class RankingTeam {
  int? rank;
  int? teamId;
  String? teamLogo;
  String? teamName;
  int? totalMatches;
  int? winMatches;
  int? loseMatches;
  int? drawMatches;
  int? totalScore;
  int? goal;
  int? fumble;
  double? winRate;
  int? goalDiff;
  String? winDiff;
  String? recentRecord;
  String? explain;

  RankingTeam(
      {this.rank,
      this.teamId,
      this.teamLogo,
      this.teamName,
      this.totalMatches,
      this.winMatches,
      this.loseMatches,
      this.drawMatches,
      this.totalScore,
      this.goal,
      this.fumble,
      this.winRate,
      this.goalDiff,
      this.winDiff,
      this.recentRecord,
      this.explain});

  RankingTeam.fromSoccer(Map<String, dynamic> json) {
    rank = json['rank'];
    teamId = json['teamId'];
    teamLogo = json['teamLogo'];
    teamName = json['teamName'];
    totalMatches = json['totalCount'];
    winMatches = json['winCount'];
    loseMatches = json['loseCount'];
    drawMatches = json['drawCount'];
    totalScore = json['integral'];
    goal = json['getScore'];
    fumble = json['loseScore'];
    goalDiff = json['goalDifference'];
    explain = json['deductionExplainCn'];
  }

  RankingTeam.fromBasketball(Map<String, dynamic> json) {
    rank = json['position'];
    teamId = json['teamId'];
    teamLogo = json['teamLogo'];
    teamName = json['teamName'];
    winMatches = json['won'];
    loseMatches = json['lost'];
    winRate = json['wonRate'];
    winDiff = json['gameBack'];
    recentRecord = json['streaks'];
  }

  Map<String, dynamic> toJsonSoccer() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['rank'] = this.rank;
    data['teamId'] = this.teamId;
    data['teamLogo'] = this.teamLogo;
    data['teamName'] = this.teamName;
    data['totalCount'] = this.totalMatches;
    data['winCount'] = this.winMatches;
    data['loseCount'] = this.loseMatches;
    data['drawCount'] = this.drawMatches;
    data['integral'] = this.totalScore;
    data['getScore'] = this.goal;
    data['loseScore'] = this.fumble;
    data['goalDifference'] = this.goalDiff;
    data['deductionExplainCn'] = this.explain;
    return data;
  }

  List<String> get dataLineSoccer => [
        "${totalMatches ?? 0}",
        "${winMatches ?? 0}/${drawMatches ?? 0}/${loseMatches ?? 0}",
        "${goal ?? 0}/${fumble ?? 0}",
        "${goalDiff ?? 0}",
        "${totalScore ?? 0}"
      ];

  List<String> get dataLineBasketball => [
        "${winMatches ?? 0}/${loseMatches ?? 0}",
        "${winRate ?? 0}",
        "${winDiff ?? 0}",
        recentRecord ?? '-'
      ];
}

class RankingQualify {
  int? beginRank;
  String? color;
  int? endRank;
  String? name;
  String? tagColor;

  RankingQualify({
    this.beginRank,
    this.color,
    this.endRank,
    this.name,
    this.tagColor,
  });

  RankingQualify.fromSoccer(Map<String, dynamic> json) {
    beginRank = json['beginRank'];
    color = json['color'];
    endRank = json['endRank'];
    tagColor = json['tagColor'];
    name = json['leagueName'];
  }

  RankingQualify.fromBasketball(Map<String, dynamic> json) {
    beginRank = json['begin'];
    color = json['color'];
    endRank = json['end'];
    name = json['name'];
    tagColor = json['tagColor'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['begin'] = this.beginRank;
    data['color'] = this.color;
    data['end'] = this.endRank;
    data['name'] = this.name;
    data['tagColor'] = this.tagColor;
    data['leagueName'] = this.name;
    return data;
  }
}
